package leetcode.weekly.week291;

import java.util.Arrays;

import leetcode.IDebug;

public class Solution5 implements IDebug {

	public long appealSum(String s) {
		char[] cs = s.toCharArray();
		int len = s.length();
		long ans = 0;
		int[] hash = new int[26];
		long cnt = 0;
		for (int i = 1; i <= len; i++) {
			int cur = cs[i - 1] - 'a';
			if (hash[cur] == 0) {
				cnt++;
			}
			hash[cur]++;
			int[] tmp = Arrays.copyOf(hash, 26);
			long curAns = this.find(cs, i, tmp, cnt);
			ans += curAns;
		}
		return ans;
	}

	private long find(char[] cs, int index, int[] hash, long cnt) {
		int p = index;
		long ans = cnt;
		while (p < cs.length) {
			int cur = cs[p] - 'a';
			if (hash[cur]++ == 0) {
				cnt++;
			}
			int sub = cs[p - index] - 'a';
			if (--hash[sub] == 0) {
				cnt--;
			}
			ans += cnt;
			p++;
		}
		return ans;
	}

	@Override
	public void debug4() {
		long ans = this.appealSum("abb");
		System.out.println(ans);
	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution5 so = new Solution5();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
